package com.link4a.doms.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.link4a.doms.dao.Page;
import com.link4a.doms.dao.handler.LicenseHandler;
import com.link4a.doms.model.License;
import com.link4a.doms.vo.LicenseCondition;

@Service
public class LicenseService {
	
	@Autowired
	private LicenseHandler licenseHandler;

	public License findById(int id) {
		return licenseHandler.findById(id);
	}
	
	public Page<License> findPage(LicenseCondition orderCondition) {
		return licenseHandler.findPage(orderCondition);
	}
	
	public void disable(int id) {
		licenseHandler.disable(id);
	}
	
	public void enable(int id) {
		licenseHandler.enable(id);
	}
	
	public boolean onCheck(String mac, String license) {
		License li = licenseHandler.findByLicense(license);
		if (!mac.equals(li.getMac())) {
			//License与MAC地址不匹配
			return false;
		}
		
		if (License.STATUS_DISABLED.equals(li.getStatus()) || License.STATUS_EXPIRED.equals(li.getStatus())) {
			//License状态不可用
			return false;
		}
		
		if (License.STATUS_IDLE.equals(li.getStatus())) {
			licenseHandler.activate(li.getId());
			return true;
		}
		
		licenseHandler.refreshLastCheckTime(li.getId());
		return true; 
	}
	
}